﻿<?php
include 'server_info.php';

	// 创建数据库连接
    $dbc= new mysqli($hostip.':'.$port,$username,$password,$db);
    if(!$dbc)  {
        echo"数据库链接错误!";
    }

// 获取并处理用户提交的表单数据
$mysqluser = mysqli_real_escape_string($dbc,htmlspecialchars($_POST["mysqluser"]));
$email = mysqli_real_escape_string($dbc,htmlspecialchars($_POST["email"]));
$pw = mysqli_real_escape_string($dbc,htmlspecialchars($_POST["pw"]));
$pw2 = mysqli_real_escape_string($dbc,htmlspecialchars($_POST["pw2"]));
$ip = getenv("REMOTE_ADDR");
#$expansion = mysql_real_escape_string($_POST["expansion"]);

// 验证表单数据是否完整且密码匹配
if (empty($mysqluser) || empty($pw) || empty($pw2) ||  empty($email) || $pw != $pw2 )
{
	echo "<script>alert('你的注册信息填写不完整！');location.href='index.php';</script>";
	exit();
}



// 检查用户名是否已存在
$result = mysqli_query($dbc,"SELECT * FROM account WHERE username LIKE '$mysqluser'");
$menge2 = mysqli_num_rows($result);

if ($menge2 == 0)
{
	// 插入新用户数据
	$entry = "INSERT INTO account (username, sha_pass_hash, email, expansion) VALUES (UPPER('".$mysqluser."'), SHA1(CONCAT(UPPER('".$mysqluser."'),':',UPPER('".$pw."'))),'".$email."','4')";
	$enter = mysqli_query($dbc,$entry);

	if ($enter == true)
	{
		echo "<script>alert('恭喜！你的账号注册成功，你现在可以登陆服务器了。');location.href='index.php';</script>";exit();
	}
	else
	{
		echo "<script>alert('账号注册时发生错误，请联系游戏管理员。');location.href='index.php';</script>";exit();
	}

}
else
{
	echo "<script>alert('你注册的账号已经存在了，请更换一个账户进行注册。');location.href='index.php';</script>";exit();
}

?>
